﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NHibernate;
using Pcme.Info.MvcMusicStore.ViewModels;
using Pcme.Info.MvcMusicStore.Models;
using Pcme.Info.MvcMusicStore.Helpers;
using NHibernate.Criterion;
using Pcme.Info.MvcMusicStore.Filter;

namespace Pcme.Info.MvcMusicStore.Controllers
{
    
    public class StoreController : Controller
    {
        ISession session = NHibernateHelper.GetSession();

        //
        // GET: /Store/
        
        public ActionResult Index()
        {
            // Retrieve list of Genres from database
            var genres = session.CreateQuery("select g.Name from Genre g").List<string>();

            // Set up our ViewModel
            var viewModel = new StoreIndexViewModel()
            {
                Genres = genres,
                NumberOfGenres = genres.Count
            };

            // Return the view
            return View(viewModel);
        }

        //
        // GET: /Store/Browse?Genre=Disco

        public ActionResult Browse(string genre)
        {
            // Retrieve Genre and its Associated Albums from database
            var genreModel = session.CreateCriteria<Genre>()
                .Add(Restrictions.Eq("Name", genre))
                .UniqueResult<Genre>();

            var viewModel = new StoreBrowseViewModel()
            {
                Genre = genreModel,
                Albums = genreModel.Albums
            };

            return View(viewModel);
        }

        //
        // GET: /Store/Details/5

        public ActionResult Details(int id)
        {
            var album = session.Get<Album>(id, LockMode.Read);

            return View(album);
        }

        //
        // GET: /Store/GenreMenu

        [ChildActionOnly]
        public ActionResult GenreMenu()
        {
            var genres = session.CreateCriteria<Genre>().List<Genre>();

            return View(genres);
        }
    }
}
